//
//  MHBannerViewController.m
//  ManHua
//
//  Created by 赵永泉 on 15/10/27.
//  Copyright © 2015年 蓝欧科技. All rights reserved.
//

#import "MHBannerViewController.h"
#import "MHComicIntroduct.h"
#import <UIButton+WebCache.h>

@interface MHBannerViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) NSTimer *timer;
@end

@implementation MHBannerViewController

- (UIView *)contentView
{
    if (_contentView == nil) {
        CGFloat scrollH = 44.0 / 64.0 * Width;
        
        self.contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Width, scrollH)];
        [self addSubview:self.contentView];
        
        self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, Width, scrollH)];
        [self.contentView addSubview:self.scrollView];
        self.scrollView.pagingEnabled = YES;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        self.scrollView.delegate = self;
        
        self.pageControl = [[UIPageControl alloc]init];
        [self.contentView addSubview:self.pageControl];
        [self.pageControl addTarget:self action:@selector(doTapPageControl:) forControlEvents:UIControlEventValueChanged];
        
        self.scrollView.backgroundColor = [UIColor whiteColor];

        [self addTimer];
    }
    return _contentView;
}

- (void)setImageArray:(NSArray *)imageArray
{
    _imageArray = imageArray;
    self.scrollView.contentSize = CGSizeMake(Width * imageArray.count, 0);
    self.pageControl.numberOfPages = imageArray.count;
    CGFloat pageControlW = self.pageControl.numberOfPages * 15;
    self.pageControl.frame = CGRectMake(Width - 10 - pageControlW, CGRectGetMaxY(self.scrollView.frame) - 30, pageControlW, 30);
    
    for (int i = 0; i < imageArray.count; i++) {
        UIButton *carouselButton = [[UIButton alloc]initWithFrame:CGRectMake(Width * i, 0, Width, self.scrollView.frame.size.height)];
        carouselButton.tag = 100 * i;
        carouselButton.backgroundColor = [UIColor lightGrayColor];
        [carouselButton sd_setImageWithURL:[NSURL URLWithString:[imageArray[i] BookIconOtherURL]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"f3c2d7039245d688094612bba6c27d1ed21b242c.jpg"]];
        [carouselButton addTarget:self action:@selector(didClickCarouselButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:carouselButton];
    }
    
}

- (void)didClickCarouselButton:(UIButton *)button
{
    NSInteger i = button.tag / 100;
    if (self.buttonClickBlock != nil) {
        self.buttonClickBlock(i);
    }
}


- (void)doTapPageControl:(UIPageControl *)pageControl
{
    [self.scrollView setContentOffset:CGPointMake(Width * pageControl.currentPage, 0) animated:YES];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self removeTimer];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger i = scrollView.contentOffset.x / Width;
    self.pageControl.currentPage = i;
    [self addTimer];
}

- (void)addTimer
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
}
- (void)removeTimer
{
    [self.timer invalidate];
}

- (void)nextImage
{
    if (self.pageControl.currentPage != self.imageArray.count - 1) {
        [self.scrollView setContentOffset:CGPointMake(Width * (self.pageControl.currentPage + 1), 0) animated:YES];
        self.pageControl.currentPage = self.pageControl.currentPage + 1;
    }
    else
    {
        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        self.pageControl.currentPage = 0;
    }
}

@end
